import pygame, sys
from settings import *
from level import Level
from game_data import level_1
from ui import UI
import os

# Pygame setup
pygame.init()
screen = pygame.display.set_mode((screen_width,screen_height))
clock = pygame.time.Clock()
# 设置logo和界面标题
game_icon = pygame.image.load('../graphics/changbao.png')
pygame.display.set_icon(game_icon)
pygame.display.set_caption('Level_1')

# function for outputting text onto the screen
def draw_text(text,font,text_col,x,y):
    img = font.render(text, True, text_col) 
    screen.blit(img,(x,y))

class Game:
    def __init__(self):
        # level setup
        self.level = Level(level_1,screen, self.change_coins)

        # ui setup
        self.coins = 0
        #self.show = True

        # User Interface
        self.ui = UI(screen)

    def change_coins(self,amount):
        self.coins += amount
    
    '''# def show_charactercard(x,y):
    #     waiting = True
    #     waited_time = pygame.time.get_ticks()
    #     while waiting:
    #         for event in pygame.event.get():
    #             if event.type == pygame.QUIT:
    #                 pygame.quit()
    #                 quit()
    #         draw_text('Salivary Enzyme',font_small,'white',x,y)
    #         pygame.display.update()
    #         clock.tick(15)
    #         if pygame.time.get_ticks() - waited_time > 2000:
    #             waiting = False
    
    # def card(self):
    #     if show:
    #         for ez in self.level.enzyme_sprites:
    #             self.show_charactercard(ez.rect.left,ez.rect.top-30)
    #             show = False'''  
    
    def run(self):
        self.level.run()
        self.ui.show_coins(self.coins)
        '''self.card()'''
        
game = Game()

# font
font_small = pygame.font.SysFont('Lucida Sans', 15)
font_big = pygame.font.SysFont('Lucida Sans', 40)
fade_counter = 0


while True:
    # ending animation
    def ending():
        global fade_counter
        # lose screen
        if (game.level.status == -1) or ((game.level.status == 1) and (game.coins < 7)):
            if fade_counter < screen_width / 2:
                fade_counter += 10
                pygame.draw.rect(screen, 'black', (0, 0, fade_counter, screen_height))
                pygame.draw.rect(screen, 'black', (screen_width - fade_counter, 0, screen_width, screen_height))
            else: 
                screen.fill('black') # fill the screen with black or the after the animation ending the screen will be as before
                draw_text('TRY AGAIN!',font_big,'white',470,250)
                draw_text('SCORE:' + str(game.coins),font_big,'white',500,300)
                # exit
                draw_text('PRESS SPACE TO EXIT',font_big,'white',390,350)
                # play again
                draw_text('PRESS ENTER TO PLAY Again',font_big,'white',320,400)
                key = pygame.key.get_pressed()
                if key[pygame.K_SPACE]:
                    pygame.quit()
                    sys.exit()
                elif key[pygame.K_RETURN]:
                    pygame.quit()
                    os.system('python main.py')
                    quit()
                
        # win screen
        elif  (game.level.status == 1) and (game.coins >= 7):   
            if fade_counter < screen_width / 2:
                fade_counter += 10
                pygame.draw.rect(screen, 'black', (0, 0, fade_counter, screen_height))
                pygame.draw.rect(screen, 'black', (screen_width - fade_counter, 0, screen_width, screen_height))
            else: 
                screen.fill('black')
                draw_text('YOU WIN!',font_big,'yellow',500,250)
                draw_text('SCORE:' + str(game.coins),font_big,'white',500,300)
                draw_text('PRESS SPACE TO EXIT',font_big,'white',390,350)
                draw_text('Press ENTER TO THE NEXT LEVEL',font_big,'white',320,400)
                key = pygame.key.get_pressed()
                if key[pygame.K_SPACE]:
                    pygame.quit()
                    sys.exit()
                elif key[pygame.K_RETURN]:
                    pygame.quit()
                    os.system('cd ../../level_2/mainpy & python main.py')
                    quit()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill('black')
    game.run()
    ending()

    pygame.display.update()
    clock.tick(60)